package phoenics.manpads.stinger.controller;

import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.stage.Modality;
import org.springframework.beans.factory.annotation.Autowired;
import phoenics.manpads.stinger.StingerFxApplicaction;
import phoenics.manpads.stinger.config.IconConfig;
import phoenics.manpads.stinger.dao.ProjectDao;
import phoenics.manpads.stinger.domain.po.Project;
import phoenics.manpads.stinger.model.ProjectTreeModel;
import phoenics.manpads.stinger.view.EditProjectModalDialog;
import phoenics.manpads.stinger.view.TableView;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * 主窗口
 *
 * @author Phoenics Chow
 * on 2019年01月22日
 */
@FXMLController
public class MainLaunchController  implements Initializable {
    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MainLaunchController.class);
    @Autowired
    TableView tableView;
    @FXML
    Tab functionTab;
    @FXML
    TreeView<ProjectTreeModel> projectTree;
    @FXML
    TabPane functionView;
    @FXML
    ContextMenu funMenu;
    @Autowired
    ProjectDao projectDao;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        reloadProjectTree();
        projectTree.getSelectionModel().selectedItemProperty().addListener((ob,oldItem,newItem)->{
            String funId=newItem.getValue().getFunId();
            if("table".equals(funId)){
                functionTab.setContent(tableView.getView());
            }else if("nexus".equals(funId)){
                logger.info("-----表间关系123--------");
            }



        });

    }
    @FXML
    public void createProject(ActionEvent event){
        EditProjectController.projectModel.clear();
        StingerFxApplicaction.showView(EditProjectModalDialog.class, Modality.APPLICATION_MODAL);

    }
    public void reloadProjectTree(){
        List<Project> projects=projectDao.selectAll();
        final TreeItem<ProjectTreeModel> dummyRoot = new TreeItem<>();
        projectTree.setShowRoot(false);
        projectTree.setRoot(dummyRoot);
        dummyRoot.getChildren().clear();
        projects.forEach(p->{
            ProjectTreeModel ti=new ProjectTreeModel();
            ti.setName(p.getProjectCnName());
            ti.setData(p);
            TreeItem<ProjectTreeModel> t=new TreeItem<>(ti, IconConfig.projectIcon());
            dummyRoot.getChildren().add(configProjectNode(t));

        });

    }
    private TreeItem<ProjectTreeModel> configProjectNode(TreeItem<ProjectTreeModel> treeItemPartent){
        TreeItem<ProjectTreeModel> brother1=configProjectFunctionNode("table","数据库表",treeItemPartent.getValue().getData(),IconConfig.tableIcon());
        TreeItem<ProjectTreeModel> brother2=configProjectFunctionNode("nexus","表间关系",treeItemPartent.getValue().getData(),IconConfig.raIcon());
        treeItemPartent.getChildren().addAll(brother1,brother2);
        return treeItemPartent;
    }
    private TreeItem<ProjectTreeModel> configProjectFunctionNode(String funId, String name,Object object,  ImageView icon){
        ProjectTreeModel tableRoot=new ProjectTreeModel();
        tableRoot.setFunId(funId);
        tableRoot.setName(name);
        tableRoot.setData(object);
        return new TreeItem<>(tableRoot, icon);
    }
    public void showingMenu(){
        funMenu.getItems().get(0).setVisible(true);
    }

    public TabPane getFunctionView(){
       return this.functionView;
    }

}
